#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char const* argv[])
{
    int data[1000000], a, b, n, ans, i, start_flg, start_cnt;

    while (scanf("%d %d %d", &a, &b, &n) != EOF) {
        if (0 == a && 0 == b && 0 == n) break;

        memset(data, 0, sizeof(int)*1000000);
        data[1] = 1;
        data[2] = 1;

        start_flg = 0; ans = -1; start_cnt = 0;
        for (i = 3; i < 1000000; i++) {
            data[i] = (a * data[i-1] + b * data[i-2]) % 7;
            if (0 == data[i] && start_cnt < 2) {
                start_flg = i;
                start_cnt++;
            } else if (0 == data[i] && start_cnt >= 2 && data[i-1] == data[start_flg-1]) {
                ans = i - start_flg;
                break;
            }
        }

        printf("%d\n", ans);
    }

    return 0;
}
